<?php
/**
 * 
 * 轮播器信息
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-4-28
 * model() 创建模型对象，静态
 * tableName() 返回表名
*/
class Adver extends CActiveRecord {
	public $page_list = array();//分页数组
	public $infos_count = 0;//统计查询个数
	/**
	 *返回当前模型对象的方法 
	 */
	public static function model($className = __CLASS__){
		return parent::model($className);
	}
	/**
	 * 返回当前表名
	 */
	public function tableName(){
		return '{{turn}}';
	}


	/**
	 *更新排序信息
	 */
	public function updateAdver($adver_id,$sort){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{adver}} SET adver_sort = $sort WHERE adver_id = $adver_id";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 *更新排序信息(品牌汇总)
	 */
	public function updateAdverBrand($adverbrand_id,$sort){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{adverbrand}} SET adverbrand_sort = $sort WHERE adverbrand_id = $adverbrand_id";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	
	/**
	 * 新增新品上市广告
	 */
	public function addAdver($addAdver){
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{adver}}(`adver_name`,`adver_url`,`adver_thumbnail_url`,`adver_position`,`adver_rec`,`adver_sort`,`active`) VALUES ('".$addAdver['adver_name']."','".$addAdver['adver_url']."','".$addAdver['adver_thumbnail_url']."','".$addAdver['adver_position']."','".$addAdver['adver_rec']."','".$addAdver['adver_sort']."','".$addAdver['active']."')";
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 * 新增品牌汇总广告
	 */
	public function addAdverBrand($addAdver){
		$cmodel = new CCCModel();
		$_sql = "INSERT INTO {{adverbrand}}(`adverbrand_title`,`adverbrand_name`,`adverbrand_thumbnail_url`,`adverbrand_url`,`adverbrand_position`,`adverbrand_sort`,`active`) VALUES ('".$addAdver['adverbrand_title']."','".$addAdver['adverbrand_name']."','".$addAdver['adverbrand_thumbnail_url']."','".$addAdver['adverbrand_url']."','".$addAdver['adverbrand_position']."','".$addAdver['sort']."','".$addAdver['active']."')";
		$row = $cmodel->upd($_sql);
		return $row;
	}

	
	/**
	 * 广告修改
	 */
	public function AdverUpdate($recPost,$adver_id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{adver}} set 
				adver_name='".$recPost['adver_name']."',
				adver_url='".$recPost['adver_url']."',
				adver_thumbnail_url = '".$recPost['adver_thumbnail_url']."',
				adver_position='".$recPost['adver_position']."',
				adver_rec='".$recPost['adver_rec']."',
				adver_sort='".$recPost['adver_sort']."',
				active=".$recPost['active']."
				WHERE adver_id=".$adver_id;
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 * 品牌汇总列表修改
	 */
	public function AdverBrandUpdate($recPost,$adverbrand_id){
		$cmodel = new CCCModel();
		$_sql = "UPDATE {{adverbrand}} set 
				adverbrand_title='".$recPost['adverbrand_title']."',
				adverbrand_name='".$recPost['adverbrand_name']."',
				adverbrand_url = '".$recPost['adverbrand_url']."',
				adverbrand_thumbnail_url='".$recPost['adverbrand_thumbnail_url']."',
				adverbrand_position='".$recPost['adverbrand_position']."',
				adverbrand_sort='".$recPost['sort']."',
				active=".$recPost['active']."
				WHERE adverbrand_id=".$adverbrand_id;
		$row = $cmodel->upd($_sql);
		return $row;
	}

	/**
	 * 查找单条记录
	 */
	public function getAdverById($adver_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{adver}} WHERE adver_id=".$adver_id;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/**
	 * 查找单条品牌汇总记录
	 */
	public function getAdverBrandById($adverbrand_id){
		$cmodel = new CCCModel();
		$_sql = "SELECT * FROM {{adverbrand}} WHERE adverbrand_id=".$adverbrand_id;
		$result = $cmodel->sel($_sql);
		return $result;
	}
	
	/**
	 * 删除广告
	 */
	public function AdverDel($adver_id){
		$cmodel = new CCCModel();
		$_sql = "DELETE from {{adver}} WHERE adver_id=".$adver_id;
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 * 删除品牌汇总广告
	 */
	public function AdverBrandDel($adverbrand_id){
		$cmodel = new CCCModel();
		$_sql = "DELETE from {{adverbrand}} WHERE adverbrand_id=".$adverbrand_id;
		$row = $cmodel->upd($_sql);
		return $row;
	}
	
	/**
	 * 分页+搜索
	 */
	public function pageSearch($getInfo){
		$_sql = '';
		$cmodel = new CCCModel();
		$_sql .= "SELECT * FROM {{adver}} WHERE 1=1";
		$nextpage = "";
		if ($getInfo){     
			if($_POST['active'] != 3){
				$nextpage .= "/active=".$getInfo['active'];
				$_sql.=" AND active=".$getInfo['active'];
			}
			if($_POST['adver_position'] != 3){
				$nextpage .= "/adver_position=".$getInfo['adver_position'];
				$_sql.=" AND adver_position=".$getInfo['adver_position'];
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
		
			$_sql .= " ORDER BY adver_sort ASC";
			$infos = $cmodel->sel($_sql);
	
			return $infos;
	}
	
	
 	 /**
	 * 分页+搜索
	 */
	public function pageSearchBrand($getInfo){
		$_sql = '';
		$cmodel = new CCCModel();
		$_sql .= "SELECT * FROM {{adverbrand}} WHERE 1=1";
		$nextpage = "";
		if ($getInfo){     
			if($_POST['active'] != 3){
				$nextpage .= "/active=".$getInfo['active'];
				$_sql.=" AND active=".$getInfo['active'];
			}
			if($_POST['adverbrand_position'] != 5){
				$nextpage .= "/adverbrand_position=".$getInfo['adverbrand_position'];
				$_sql.=" AND adverbrand_position=".$getInfo['adverbrand_position'];
			}
			$_sql_count = str_replace('SELECT *','SELECT count(*)',$_sql);
		}
		
			$_sql .= " ORDER BY adverbrand_position ASC";
			$infos = $cmodel->sel($_sql);
	
			return $infos;
	}
	
	/**
	 * 是否推荐的切换
	 */
	public function setRec($id,$adver_rec){
		$cmodel = new CCCModel();
		if($adver_rec == 1){
			$_sql = "UPDATE {{adver}} set adver_rec=0  WHERE adver_id = ".$id;
			$row = $cmodel->upd($_sql);
		}else{
			$_sql = "UPDATE {{adver}} set adver_rec=1  WHERE adver_id = ".$id;
			$row = $cmodel->upd($_sql);
		}
		return $row;
	}
	
	/**
	 * 检测是否已经存在
	 */
	public function checkExist($addInfo){
		$cmodel = new CCCModel();
		$result = $cmodel -> sel("SELECT adver_id FROM {{adver}} where adver_name ='".$addInfo['adver_name']."'");
		return $result;
	}
	
	/**
	 * 检测是否已经存在
	 */
	public function checkExistBrand($addInfo){
		$cmodel = new CCCModel();
		$result = $cmodel -> sel("SELECT adverbrand_id FROM {{adverbrand}} where adverbrand_name = '".$addInfo['adverbrand_name']."' OR adverbrand_title ='".$addInfo['adverbrand_title']."'");
		return $result;
	}
	
	/**
	 * 按照sort查询最新上市+横条广告所有数据
	 */
	public function advernew(){
		$all_model = new CCCModel();
		$_sql = "SELECT * FROM {{adver}}  ORDER BY adver_sort ASC ";
		$result = $all_model->sel($_sql);
		return $result;
	}
	
/**
	 * 按照sort查询品牌汇总列广告
	 */
	public function adverbrand(){
		$all_model = new CCCModel();
		$_sql = "SELECT * FROM {{adverbrand}}  ORDER BY adverbrand_position ASC ";
		$result = $all_model->sel($_sql);
		return $result;
	}

}
	
?>